package cn.tedu.ivos.dictOption.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.dictOption.pojo.dto.DictOptionSaveParam;
import cn.tedu.ivos.dictOption.pojo.dto.DictOptionQuery;
import cn.tedu.ivos.dictOption.pojo.vo.DictOptionVO;
import cn.tedu.ivos.dictOption.service.DictOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v1/dictOption")
public class DictOptionController {
    @Autowired
    private DictOptionService dictOptionService;
    @RequestMapping("/select")
    public JsonResult selectDictOption(DictOptionQuery dictOptionQuery){
        log.debug("开始处理【查询字典项】的请求，参数：{}",dictOptionQuery);
        List<DictOptionVO> dictOptionVOList =dictOptionService.selectDictOption(dictOptionQuery);
        return JsonResult.ok(dictOptionVOList);
    }
    @PostMapping("/save")
    public JsonResult saveDictOption(@RequestBody DictOptionSaveParam dictOptionSaveParam){
        log.debug("开始处理【保存字典项】的请求，参数：{}", dictOptionSaveParam);
        int row=dictOptionService.saveDictOption(dictOptionSaveParam);
        if(row==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"保存失败");
        }
        return JsonResult.ok();
    }
    @PostMapping("/delete/{id}")
    public JsonResult deleteDictOption(@PathVariable Long id){
        log.debug("开始处理【删除字典项】的请求，参数：{}",id);
        int row=dictOptionService.deleteDictOption(id);
        if(row==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"删除失败");
        }
        return JsonResult.ok();
    }
    @GetMapping("/selectDictOptionByCode/{code}")
    public JsonResult selectDictOptionByCode(@PathVariable String code){
        log.debug("开始处理【根据字典编码查询字典项】的请求，参数：{}",code);
        List<DictOptionVO> dictOptionVOList =dictOptionService.selectDictOptionByCode(code);
        if(dictOptionVOList!=null){
            log.debug("根据字典编码查询字典项，结果为：{}", dictOptionVOList);
            return JsonResult.ok(dictOptionVOList);
        }
        return new JsonResult(StatusCode.OPERATION_FAILED,"查询失败");
    }
}
